package com.iwinding.ladygg.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMChatManager;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.ui.EaseConversationListFragment;
import com.iwinding.ladygg.constant.Constant;
import com.iwinding.ladygg.ui.act.ChatActivity;

import java.util.List;


/**
 * 消息模块
 * Created by hezhiyong on 2017/3/13.
 */

public class NoticeFragment extends EaseConversationListFragment implements AdapterView.OnItemClickListener{
    EMChatManager emChatManager;

    @Override
    protected void initView() {
        super.initView();
        hideTitleBar();
        query.setVisibility(View.GONE);
        clearSearch.setVisibility(View.GONE);

        conversationListView.setOnItemClickListener(this);
        emChatManager = EMClient.getInstance().chatManager();
        emChatManager.addConversationListener(convListener);
        emChatManager.addMessageListener(new EMMessageListener() {
            @Override
            public void onMessageReceived(List<EMMessage> list) {
                //收到消息
                refresh();
            }

            @Override
            public void onCmdMessageReceived(List<EMMessage> list) {
                //收到透传消息
                refresh();
            }

            @Override
            public void onMessageRead(List<EMMessage> list) {
                //收到已读回执
                refresh();
            }

            @Override
            public void onMessageDelivered(List<EMMessage> list) {
                //收到已送达回执
                refresh();
            }

            @Override
            public void onMessageChanged(EMMessage emMessage, Object o) {
                //消息状态变动
                refresh();
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        EMConversation conversation = conversationListView.getItem(position);
        String username = conversation.conversationId();
        if (username.equals(EMClient.getInstance().getCurrentUser()))
            Toast.makeText(getActivity(), "不是你的对话", Toast.LENGTH_SHORT).show();
        else {
            // start chat acitivity
            Intent intent = new Intent(getActivity(), ChatActivity.class);
            if(conversation.isGroup()){
                if(conversation.getType() == EMConversation.EMConversationType.ChatRoom){
                    // it's group chat
                    intent.putExtra(Constant.EXTRA_CHAT_TYPE, Constant.CHATTYPE_CHATROOM);
                }else{
                    intent.putExtra(Constant.EXTRA_CHAT_TYPE, Constant.CHATTYPE_GROUP);
                }

            }
            // it's single chat
            intent.putExtra(Constant.EXTRA_USER_ID, username);
            startActivity(intent);
        }
    }
}
